package com.xyz.practice.jdbc.databasemetadata;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


class Converter {

  static List<ColumnPrivilege> convertColumnPrivileges( ResultSet rs ) throws SQLException {
    if ( rs != null ) {
      List<ColumnPrivilege> columnPrivileges = new ArrayList<ColumnPrivilege>();
      while ( rs.next() ) {
        ColumnPrivilege columnPrivilege = new ColumnPrivilege();
        columnPrivilege.setColumnName( rs.getString( "COLUMN_NAME" ) );
        columnPrivilege.setGrantee( rs.getString( "GRANTEE" ) );
        columnPrivilege.setGrantor( rs.getString( "GRANTOR" ) );
        columnPrivilege.setIsGrantable( rs.getString( "IS_GRANTABLE" ) );
        columnPrivilege.setPrivilege( rs.getString( "PRIVILEGE" ) );
        columnPrivilege.setTableCat( rs.getString( "TABLE_CAT" ) );
        columnPrivilege.setTableName( rs.getString( "TABLE_NAME" ) );
        columnPrivilege.setTableSchem( rs.getString( "TABLE_SCHEM" ) );
        columnPrivileges.add( columnPrivilege );
      }
      return columnPrivileges;
    }
    return null;
  }

  static List<ClientInfoProperty> convertClientInfoProperties( ResultSet rs ) throws SQLException {
    if ( rs != null ) {
      List<ClientInfoProperty> clientInfoPropertys = new ArrayList<ClientInfoProperty>();
      while ( rs.next() ) {
        ClientInfoProperty clientInfoProperty = new ClientInfoProperty();
        clientInfoProperty.setDefaultValue( rs.getString( "DEFAULT_VALUE" ) );
        clientInfoProperty.setDescription( rs.getString( "DESCRIPTION" ) );
        clientInfoProperty.setMaxLen( rs.getInt( "MAX_LEN" ) );
        clientInfoProperty.setName( rs.getString( "NAME" ) );
        clientInfoPropertys.add( clientInfoProperty );
      }
      return clientInfoPropertys;
    }
    return null;
  }

  static List<ProcedureColumn> convertProcedureColumns( ResultSet rs ) throws SQLException {
    if ( rs != null ) {
      List<ProcedureColumn> procedureColumns = new ArrayList<ProcedureColumn>();
      while ( rs.next() ) {
        ProcedureColumn procedureColumn = new ProcedureColumn();
        procedureColumn.setCharOctetLength( rs.getInt( "CHAR_OCTET_LENGTH" ) );
        procedureColumn.setColumnDef( rs.getString( "COLUMN_DEF" ) );
        procedureColumn.setColumnName( rs.getString( "COLUMN_NAME" ) );
        procedureColumn.setColumnType( rs.getShort( "COLUMN_TYPE" ) );
        procedureColumn.setDataType( rs.getInt( "DATA_TYPE" ) );
        procedureColumn.setIsNullable( rs.getString( "IS_NULLABLE" ) );
        procedureColumn.setLength( rs.getInt( "LENGTH" ) );
        procedureColumn.setNullable( rs.getShort( "NULLABLE" ) );
        procedureColumn.setOrdinalPosition( rs.getInt( "ORDINAL_POSITION" ) );
        procedureColumn.setPrecision( rs.getInt( "PRECISION" ) );
        procedureColumn.setProcedureCat( rs.getString( "PROCEDURE_CAT" ) );
        procedureColumn.setProcedureName( rs.getString( "PROCEDURE_NAME" ) );
        procedureColumn.setProcedureSchem( rs.getString( "PROCEDURE_SCHEM" ) );
        procedureColumn.setRadix( rs.getShort( "RADIX" ) );
        procedureColumn.setRemarks( rs.getString( "REMARKS" ) );
        procedureColumn.setScale( rs.getShort( "SCALE" ) );
        procedureColumn.setSpecificName( rs.getString( "SPECIFIC_NAME" ) );
        procedureColumn.setSqlDataType( rs.getInt( "SQL_DATA_TYPE" ) );
        procedureColumn.setSqlDatetimeSub( rs.getInt( "SQL_DATETIME_SUB" ) );
        procedureColumn.setTypeName( rs.getString( "TYPE_NAME" ) );
        procedureColumns.add( procedureColumn );
      }
      return procedureColumns;
    }
    return null;
  }

  static List<Procedure> convertProcedures( ResultSet rs ) throws SQLException {
    if ( rs != null ) {
      List<Procedure> procedures = new ArrayList<Procedure>();
      while ( rs.next() ) {
        Procedure procedure = new Procedure();
        procedure.setProcedureCat( rs.getString( "PROCEDURE_CAT" ) );
        procedure.setProcedureName( rs.getString( "PROCEDURE_NAME" ) );
        procedure.setProcedureSchem( rs.getString( "PROCEDURE_SCHEM" ) );
        procedure.setProcedureType( rs.getShort( "PROCEDURE_TYPE" ) );
        procedure.setRemarks( rs.getString( "REMARKS" ) );
        procedure.setSpecificName( rs.getString( "SPECIFIC_NAME" ) );
        procedures.add( procedure );
      }
      return procedures;
    }
    return null;
  }

  static List<FunctionColumn> convertFunctionColumns( ResultSet rs ) throws SQLException {
    if ( rs != null ) {
      List<FunctionColumn> functionColumns = new ArrayList<FunctionColumn>();
      while ( rs.next() ) {
        FunctionColumn functionColumn = new FunctionColumn();
        functionColumn.setCharOctetLength( rs.getInt( "CHAR_OCTET_LENGTH" ) );
        functionColumn.setColumnName( rs.getString( "COLUMN_NAME" ) );
        functionColumn.setColumnType( rs.getShort( "COLUMN_TYPE" ) );
        functionColumn.setDataType( rs.getInt( "DATA_TYPE" ) );
        functionColumn.setFunctionCat( rs.getString( "FUNCTION_CAT" ) );
        functionColumn.setFunctionName( rs.getString( "FUNCTION_NAME" ) );
        functionColumn.setFunctionSchem( rs.getString( "FUNCTION_SCHEM" ) );
        functionColumn.setIsNullable( rs.getString( "IS_NULLABLE" ) );
        functionColumn.setLength( rs.getInt( "LENGTH" ) );
        functionColumn.setNullable( rs.getShort( "NULLABLE" ) );
        functionColumn.setOrdinalPosition( rs.getInt( "ORDINAL_POSITION" ) );
        functionColumn.setPrecision( rs.getInt( "PRECISION" ) );
        functionColumn.setRadix( rs.getShort( "RADIX" ) );
        functionColumn.setRemarks( rs.getString( "REMARKS" ) );
        functionColumn.setScale( rs.getShort( "SCALE" ) );
        functionColumn.setSpecificName( rs.getString( "SPECIFIC_NAME" ) );
        functionColumn.setTypeName( rs.getString( "TYPE_NAME" ) );
        functionColumns.add( functionColumn );
      }
      return functionColumns;
    }
    return null;
  }

  static List<ReferenceKey> convertReferenceKeys( ResultSet rs ) throws SQLException {
    if ( rs != null ) {
      List<ReferenceKey> referenceKeys = new ArrayList<ReferenceKey>();
      while ( rs.next() ) {
        ReferenceKey referenceKey = new ReferenceKey();
        referenceKey.setDeferrability( rs.getShort( "DEFERRABILITY" ) );
        referenceKey.setDeleteRule( rs.getShort( "DELETE_RULE" ) );
        referenceKey.setFkcolumnName( rs.getString( "FKCOLUMN_NAME" ) );
        referenceKey.setFkName( rs.getString( "FK_NAME" ) );
        referenceKey.setFktableCat( rs.getString( "FKTABLE_CAT" ) );
        referenceKey.setFktableName( rs.getString( "FKTABLE_NAME" ) );
        referenceKey.setFktableSchema( rs.getString( "FKTABLE_SCHEM" ) );
        referenceKey.setKeySeq( rs.getShort( "KEY_SEQ" ) );
        referenceKey.setPkcolumnName( rs.getString( "PKCOLUMN_NAME" ) );
        referenceKey.setPkName( rs.getString( "PK_NAME" ) );
        referenceKey.setPktableCat( rs.getString( "PKTABLE_CAT" ) );
        referenceKey.setPktableName( rs.getString( "PKTABLE_NAME" ) );
        referenceKey.setPktableSchem( rs.getString( "PKTABLE_SCHEM" ) );
        referenceKey.setUpdateRule( rs.getShort( "UPDATE_RULE" ) );
        referenceKeys.add( referenceKey );
      }
      return referenceKeys;
    }
    return null;
  }

  static List<Function> convertFunctions( ResultSet rs ) throws SQLException {
    if ( rs != null ) {
      List<Function> functions = new ArrayList<Function>();
      while ( rs.next() ) {
        Function function = new Function();
        function.setFunctionCat( rs.getString( "FUNCTION_CAT" ) );
        function.setFunctionName( rs.getString( "FUNCTION_NAME" ) );
        function.setFunctionSchema( rs.getString( "FUNCTION_SCHEM" ) );
        function.setFunctionType( rs.getShort( "FUNCTION_TYPE" ) );
        function.setRemarks( rs.getString( "REMARKS" ) );
        function.setSpecificName( rs.getString( "SPECIFIC_NAME" ) );
        functions.add( function );
      }
      return functions;
    }
    return null;
  }

  static List<Attribute> convertAttributes( ResultSet rs ) throws SQLException {
    if ( rs != null ) {
      List<Attribute> attributes = new ArrayList<Attribute>();
      while ( rs.next() ) {
        Attribute attribute = new Attribute();
        attribute.setAttrDef( rs.getString( "ATTR_DEF" ) );
        attribute.setAttrName( rs.getString( "ATTR_NAME" ) );
        attribute.setAttrSize( rs.getInt( "ATTR_SIZE" ) );
        attribute.setAttrTypeName( rs.getString( "ATTR_TYPE_NAME" ) );
        attribute.setCharOctetLength( rs.getInt( "CHAR_OCTET_LENGTH" ) );
        attribute.setDataType( rs.getInt( "DATA_TYPE" ) );
        attribute.setDecimalDigits( rs.getInt( "DECIMAL_DIGITS" ) );
        attribute.setIsNullable( rs.getString( "IS_NULLABLE" ) );
        attribute.setNumPrecRadix( rs.getInt( "NUM_PREC_RADIX" ) );
        attribute.setRemarks( rs.getString( "REMARKS" ) );
        attribute.setScopeCatalog( rs.getString( "SCOPE_CATALOG" ) );
        attribute.setScopeSchem( rs.getString( "SCOPE_SCHEMA" ) );
        attribute.setScopeTable( rs.getString( "SCOPE_TABLE" ) );
        attribute.setSourceDataType( rs.getShort( "SOURCE_DATA_TYPE" ) );
        attribute.setTypeCat( rs.getString( "TYPE_CAT" ) );
        attribute.setTypeName( rs.getString( "TYPE_NAME" ) );
        attribute.setTypeSchem( rs.getString( "TYPE_SCHEM" ) );

        attributes.add( attribute );
      }
      return attributes;
    }
    return null;
  }

  static List<PrimaryKey> convertPrimaryKeys( ResultSet rs ) throws SQLException {
    if ( rs != null ) {
      List<PrimaryKey> primaryKeys = new ArrayList<PrimaryKey>();
      while ( rs.next() ) {
        PrimaryKey primaryKey = new PrimaryKey();
        primaryKey.setColumnName( rs.getString( "COLUMN_NAME" ) );
        primaryKey.setKeySeq( rs.getShort( "KEY_SEQ" ) );
        primaryKey.setPkName( rs.getString( "PK_NAME" ) );
        primaryKey.setTableCat( rs.getString( "TABLE_CAT" ) );
        primaryKey.setTableName( rs.getString( "TABLE_NAME" ) );
        primaryKey.setTableSchem( rs.getString( "TABLE_SCHEM" ) );
        primaryKeys.add( primaryKey );
      }
      return primaryKeys;
    }
    return null;
  }

  static List<Schema> convertSchemas( ResultSet rs ) throws SQLException {
    if ( rs != null ) {
      List<Schema> schemas = new ArrayList<Schema>();
      while ( rs.next() ) {
        Schema schema = new Schema();
        schema.setTableSchem( rs.getString( "TABLE_SCHEM" ) );
        // schema.setTableCatalog( rs.getString( "TABLE_CATALOG" ) );
        schemas.add( schema );
      }
      return schemas;
    }
    return null;
  }

  static List<Catalog> convertCatalogs( ResultSet rs ) throws SQLException {
    if ( rs != null ) {
      List<Catalog> catalogs = new ArrayList<Catalog>();
      while ( rs.next() ) {
        catalogs.add( new Catalog( rs.getString( "TABLE_CAT" ) ) );
      }
      return catalogs;
    }
    return null;
  }

  static List<Table> convertTables( ResultSet rs ) throws SQLException {
    if ( rs != null ) {
      List<Table> tables = new ArrayList<Table>();
      while ( rs.next() ) {
        Table table = new Table();
        // table.setRefGeneration( rs.getString( "REF_GENERATION" ) );
        // MySQL not supported
        table.setRemarks( rs.getString( "REMARKS" ) );
        // table.setSelfReferencingColName( rs.getString(
        // "SELF_REFERENCING_COL_NAME" ) ); MySQL not supported
        table.setTableCat( rs.getString( "TABLE_CAT" ) );
        table.setTableName( rs.getString( "TABLE_NAME" ) );
        table.setTableSchem( rs.getString( "TABLE_SCHEM" ) );
        table.setTableType( new TableType( rs.getString( "TABLE_TYPE" ) ) );
        // table.setTypeCat( rs.getString( "TYPE_CAT" ) ); MySQL not
        // supported
        // table.setTypeName( rs.getString( "TYPE_NAME" ) ); MySQL not
        // supported
        // table.setTypeSchem( rs.getString( "TYPE_SCHEM" ) ); MySQL not
        // supported
        tables.add( table );
      }
      return tables;
    }
    return null;
  }

  static List<TableType> convertTableTypes( ResultSet rs ) throws SQLException {
    if ( rs != null ) {
      List<TableType> tableTypes = new ArrayList<TableType>();
      while ( rs.next() ) {
        tableTypes.add( new TableType( rs.getString( "TABLE_TYPE" ) ) );
      }
      return tableTypes;
    }
    return null;
  }

  static List<Column> convertColumns( ResultSet rs ) throws SQLException {
    if ( rs != null ) {
      List<Column> columns = new ArrayList<Column>();
      while ( rs.next() ) {
        Column column = new Column();
        column.setCharOctetLength( rs.getInt( "CHAR_OCTET_LENGTH" ) );
        column.setColumnDef( rs.getString( "COLUMN_DEF" ) );
        column.setColumnName( rs.getString( "COLUMN_NAME" ) );
        column.setColumnSize( rs.getInt( "COLUMN_SIZE" ) );
        column.setDataType( rs.getInt( "DATA_TYPE" ) );
        column.setDecimalDigits( rs.getInt( "DECIMAL_DIGITS" ) );
        column.setIsAutoincrement( rs.getString( "IS_AUTOINCREMENT" ) );
        column.setIsNullable( rs.getString( "IS_NULLABLE" ) );
        column.setNullable( rs.getInt( "NULLABLE" ) );
        column.setNumPrecRadix( rs.getInt( "NUM_PREC_RADIX" ) );
        column.setOriginalPosition( rs.getInt( "ORDINAL_POSITION" ) );
        column.setRemarks( rs.getString( "REMARKS" ) );
        // column.setScopeCatlog( rs.getString( "SCOPE_CATLOG" ) );
        // MySQL not supported
        column.setScopeSchema( rs.getString( "SCOPE_SCHEMA" ) );
        column.setScopeTable( rs.getString( "SCOPE_TABLE" ) );
        column.setSourceDataType( rs.getShort( "SOURCE_DATA_TYPE" ) );
        column.setTableCat( rs.getString( "TABLE_CAT" ) );
        column.setTableName( rs.getString( "TABLE_NAME" ) );
        column.setTableSchem( rs.getString( "TABLE_SCHEM" ) );
        column.setTypeName( rs.getString( "TYPE_NAME" ) );
        columns.add( column );
      }
      return columns;
    }
    return null;
  }
}
